<?php
namespace xing\verify\drive;

use xing\helper\resource\HttpHelper;
use xing\verify\BaseVerify;
use xing\verify\interfaces\VerifyMobileInterface;

class Ali365VerifyMobile extends BaseVerify implements VerifyMobileInterface
{
    private $appKey;
    private $appSecret;
    private $appCode;

    public function init($config)
    {
        $this->config = $config;
        $this->appKey = $config['appKey'] ?? null;
        $this->appSecret = $config['appSecret'] ?? null;
        $this->appCode = $config['appCode'];
        return $this;
    }

    public function query($mobile, $name, $cardNumber)
    {
        $appcode = $this->appCode;
        $headers = ["Authorization:APPCODE " . $appcode];
        $params = http_build_query(['idCard' => $cardNumber, 'mobile' => $mobile, 'realName' => $name]);
        $url = "https://zpc.market.alicloudapi.com/efficient/cellphone?" . $params;
        $result = $this->result = json_decode(HttpHelper::post($url, [], $headers));
        if (empty($result)) {
            throw new \Exception('查询无结果'); // 可能是没钱了
        }
        $success = false;
        switch ($result->error_code) {
            case 0:
                $r = $result->result->VerificationResult;
                $success = $r >= 1;
                $this->resultMsg = $success ? '验证成功' : ($r == -1 ? '身份信息不匹配' : '运营商系统中无记录');
                break;
            case 10025:
                $this->resultMsg = '运营商返回验证无结果';
                break;

        }
        return $success;
    }
}
